package com.joseph.websocket.handler;

import com.joseph.websocket.bean.SessionUser;
import com.joseph.websocket.component.SessionManager;
import com.joseph.websocket.util.AttrUtil;
import com.joseph.websocket.util.ParserUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.http.*;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 建立WS连接前用户身份验证
 */
@ChannelHandler.Sharable
public class HealthCheckHandler extends MessageToMessageDecoder<FullHttpRequest> {

    @Override
    protected void decode(ChannelHandlerContext ctx, FullHttpRequest request, List<Object> out) {
        String uri = request.uri();
        System.out.println("request url: " + uri);
        if ("/_healthCheck".equals(uri)) {
            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK);
            HttpHeaders heads = response.headers();
            heads.add(HttpHeaderNames.CONTENT_LENGTH, 0);
            ctx.channel().writeAndFlush(response);
            return;
        }
        //非健康检测
        out.add(request.retain());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("healthcheck close");
       // super.channelInactive(ctx);
    }
}
